package com.jsgt.greekfire.ui.trading.otc

import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.IS_ACTIVATION
import com.jsgt.greekfire.LOGIN_STATUS
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.OtcAdBean
import com.jsgt.greekfire.common.util.RecycleEmptyView
import com.jsgt.greekfire.common.util.XLinearLayoutManager
import com.jsgt.greekfire.factory.OTCTraddingModelFactory
import com.jsgt.greekfire.ui.home.entrance.EntranceActivity
import com.jsgt.greekfire.ui.trading.otc.adapter.OTCSellTraddingAdapter
import com.jsgt.greekfire.viewmodel.OTCTraddingViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.fragment_otctrading_sell_list.*
import org.jetbrains.anko.startActivity

private const val ARG_PARAM = "param"

class OTCTradingSellListFragment : BaseFragment() {
    private var param1: String? = null
    private var page = 1
    private var limit = 10
    private var dealNum: String? = null
    private var usdtNum: String? = null
    private var dealAmount: String? = null
    private var guid: String? = null
    private var title: String? = null

    var carList: ArrayList<OtcAdBean.ListBean>? = ArrayList()

    private val otcSellTraddingAdapter by lazy {
        OTCSellTraddingAdapter(context!!, R.layout.item_otc_tradding_sell_list)
    }
    private val otcTraddingViewModel: OTCTraddingViewModel by lazy {
        ViewModelProvider(
            this,
            OTCTraddingModelFactory()
        ).get(OTCTraddingViewModel::class.java)
    }

    companion object {

        @JvmStatic
        fun newInstance(param: String) =
            OTCTradingSellListFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM, param)
                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM)
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_otctrading_sell_list

    override fun main() {

        rvOTCSell.layoutManager = XLinearLayoutManager(context!!)
        rvOTCSell.adapter = otcSellTraddingAdapter
        otcSellTraddingAdapter?.setNewData(carList!!)

        refreshOTCSell.setRefreshHeader(MaterialHeader(context!!))
        refreshOTCSell.setOnRefreshListener {
            page = 1
            otcTraddingViewModel.getotcAdData(param1!!, 2, 1, limit, page)
        }
        refreshOTCSell.setOnLoadMoreListener {
            ++page
            otcTraddingViewModel.getotcAdData(param1!!, 2, 1, limit, page)
        }
        otcSellTraddingAdapter.addChildClickViewIds(R.id.tvBuyGFC)
        otcSellTraddingAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tvBuyGFC) {
                if (LOGIN_STATUS) {
                    if (IS_ACTIVATION == 1) {
                        ToastUtils.show(context!!, getString(R.string.account_no_activation))
                    } else {
                        otcTraddingViewModel.getotcAdStateData(
                            otcSellTraddingAdapter.data.get(position).guid!!.toString(),
                            "1"
                        )
                        var s =
                            otcSellTraddingAdapter.data.get(position).totalAmount!! - otcSellTraddingAdapter.data.get(
                                position
                            ).turnoverAmount!!
                        title = otcSellTraddingAdapter.data.get(position).coinCode!!.toString();
                        dealAmount =
                            otcSellTraddingAdapter.data.get(position).dealAmount!!.toString();
                        dealNum = otcSellTraddingAdapter.data.get(position).dealNum!!.toString();
                        guid = otcSellTraddingAdapter.data.get(position).guid!!.toString();
                        usdtNum = s.toString()
                    }
                } else {
                    goToActivity<EntranceActivity>()
                }
            }
        }
        initData()
    }

    private fun initData() {
        otcTraddingViewModel.otcAdStateData.observe(this, Observer {
            context!!.startActivity<SellOTCActivity>(
                "title" to title,
                "dealAmount" to dealAmount,
                "dealNum" to dealNum,
                "usdt" to usdtNum,
                "guid" to guid,
                "coinCode" to param1
            )
        })
        otcTraddingViewModel.otcAdData.observe(this, Observer {
            if (it.result != null) {
                if (page == 1) {
                    carList!!.clear()
                }
                carList!!.addAll(it.result?.list!!)
                otcSellTraddingAdapter.notifyDataSetChanged();
                if (it.result?.list!!.size < limit) {
                    refreshOTCSell.finishLoadMoreWithNoMoreData();
                } else {
                    refreshOTCSell.finishLoadMore();
                }
            } else {
                refreshOTCSell.finishLoadMoreWithNoMoreData();
            }

            if (carList!!.size <= 0) {
                RecycleEmptyView.showEmptyView(
                    activity as AppCompatActivity?,
                    otcSellTraddingAdapter,
                    "暂无交易",
                    R.mipmap.icon_empty_no_data
                )
            }
        })
        otcTraddingViewModel.errorLiveData.observe(this, Observer {
            ToastUtils.show(it.toString())
            if (carList!!.size <= 0) {
                RecycleEmptyView.showEmptyView(
                    activity as AppCompatActivity?,
                    otcSellTraddingAdapter,
                    "暂无交易",
                    R.mipmap.icon_empty_no_data
                )
                if (page > 1) {
                    page--;
                    refreshOTCSell.finishLoadMore(false);
                }
            }
            refreshOTCSell.finishRefresh();

        })
    }

    override fun onResume() {
        super.onResume()
        otcTraddingViewModel.getCoinlistData()
        refreshOTCSell.autoRefresh()
    }
}